package com.example.product.service.impl;

import com.example.product.dataobject.ProductInfo;
import com.example.product.enums.ProductStatus;
import com.example.product.enums.ResponseEnum;
import com.example.product.exception.ProductException;
import com.example.product.repository.ProductInfoRepository;
import com.example.product.service.ProductInfoService;
import com.example.product.utils.JsonUtil;
import com.imooc.product.common.DecreaseStockInput;
import com.imooc.product.common.ProductInfoOutput;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: sanjin
 * @date: 2019/6/12 14:09
 */
@Service
public class ProductServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoRepository productInfoRepository;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public List<ProductInfo> findUpAll() {
        return productInfoRepository.findByProductStatus(ProductStatus.UP.getCode());
    }

    @Override
    public List<ProductInfoOutput> findByProductIdIn(List<String> productIdList) {
        List<ProductInfo> productInfoList = productInfoRepository.findByProductIdIn(productIdList);
        List<ProductInfoOutput> productInfoOutputList = productInfoList.stream().map(productInfo -> {
            ProductInfoOutput productInfoOutput = new ProductInfoOutput();
            BeanUtils.copyProperties(productInfo, productInfoOutput);
            return productInfoOutput;
        }).collect(Collectors.toList());
        return productInfoOutputList;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void decreaseStock(List<DecreaseStockInput> decreaseStockInputList) {
        List<ProductInfoOutput> productInfoOutputList = new ArrayList<>();
        for (DecreaseStockInput decreaseStockInput : decreaseStockInputList) {
            // 查询商品库存
            Optional<ProductInfo> optionalProductInfo = productInfoRepository.findById(decreaseStockInput.getProductId());
            // 判断商品是否存在
            if (!optionalProductInfo.isPresent()) {
                throw new ProductException("商品不存在");
            }
            ProductInfo productInfo = optionalProductInfo.get();
            int result = productInfo.getProductStock() - decreaseStockInput.getProductQuantity();
            // 判断库存是否足够
            if (result < 0) {
                //库存不够
                throw new ProductException("商品库存不足");
            }
            productInfo.setProductStock(result);
            productInfoRepository.save(productInfo);

            // 发送MQ消息
            ProductInfoOutput productInfoOutput = new ProductInfoOutput();
            BeanUtils.copyProperties(productInfo,productInfoOutput);
            productInfoOutputList.add(productInfoOutput);

        }
        // 若购物车中商品存在问题，则会立即返回错误信息，没有问题，则发送跟新库存的消息
        amqpTemplate.convertAndSend("productInfo", JsonUtil.toJson(productInfoOutputList));
    }
}
